﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSNPSharp;

namespace TienLen
{
    public partial class PopUp : Form
    {
        Timer vistimer = new Timer();
        int state = 0;
        
        double increment = 0.04;
        object obj;
        AlertType alerttype;

        public PopUp(AlertType alerttype, params object[] data)
        {
            InitializeComponent();
            this.BackgroundImage = Image.FromFile(Application.StartupPath + @"\img\other\alert-1.jpg");

            switch (alerttype)
            {
                case AlertType.Invite:
                    {
                        infotext.Text = (data[0] as TextMessageArrivedEventArgs).Sender.Name + " has invited you to a game of " + SvrFunc.Games[(int)data[1]].Name() + ".";
                        break;
                    }
                case AlertType.Notify:
                    {
                        infotext.Text = data[0].ToString();

                        buttonok.Visible = false;
                        buttoncancel.Visible = false;
                        buttonokonly.Visible = true;

                        break;
                    }
            }

            this.alerttype = alerttype;
            obj = data[0];

            Rectangle workingarea = Screen.GetWorkingArea(this);
            this.Location = new Point(workingarea.Width - this.Size.Width, workingarea.Height - this.Size.Height);

            vistimer.Interval = 15;
            vistimer.Tick += new EventHandler(VisTimer_Tick);
            vistimer.Enabled = true;
        }

        private void VisTimer_Tick(object sender, EventArgs e)
        {
            switch (state)
            {
                case 0:
                    {
                        this.Opacity += increment;

                        if (this.Opacity == 1)
                        {
                            vistimer.Stop();
                            vistimer.Interval = 3500;
                            state++;

                            vistimer.Start();
                        }
                        break;
                    }
                case 1:
                    {
                        vistimer.Interval = 10;
                        state++;

                        break;
                    }
                case 2:
                    {
                        this.Opacity -= increment;

                        if (this.Opacity == 0)
                        {
                            this.Close();
                            vistimer.Dispose();
                        }

                        break;
                    }
            }
        }

        private void close_Click(object sender, EventArgs e)
        {
            vistimer.Stop();

            switch (alerttype)
            {
                case AlertType.Invite:
                    {
                        SvrFunc.MSN.MessageManager.SendTextMessage((obj as TextMessageArrivedEventArgs).Sender, new TextMessage("@TLC INV C"));           
                        break;
                    }
            }

            vistimer.Interval = 50;
            vistimer.Start();
        }

        private void buttoncancel_Click(object sender, EventArgs e)
        {
            close_Click(sender, e);
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            vistimer.Stop();

            switch (alerttype)
            {
                case AlertType.Invite:
                    {
                        if (((MSNClient)this.Owner).playGameButton.Enabled == false)
                        {
                            MessageBox.Show("You are already creating a game." + Environment.NewLine + "Exit the creating game to enter another players arranged room.",
                                "Unable to Accept Invitation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                            SvrFunc.MSN.MessageManager.SendTextMessage((obj as TextMessageArrivedEventArgs).Sender,new TextMessage("@TLC INV B"));

                        break;
                    }
            }

            vistimer.Interval = 50;
            vistimer.Start();
        }

        private void buttonokonly_Click(object sender, EventArgs e)
        {
            close_Click(sender, e);
        }
    }
}
